<template>
  <el-card shadow="hover" class="mt-20">
    <!-- 
      按钮禁用看category3Id
        有值，不要禁用 :disabled="false"
        没值，要禁用 :disabled="true"
    -->
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddAttr"
    >
      添加属性
    </el-button>

    <el-table border :data="attrList" class="mt-20">
      <el-table-column
        label="序号"
        type="index"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        label="属性名称"
        prop="attrName"
        width="200"
      ></el-table-column>
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            type="success"
            class="mr-10"
          >
            {{ attrValue.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template v-slot="{ row }">
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            @click="showUpdateAttr(row)"
          ></el-button>
          <el-popconfirm
            :title="`您确认要删除 ${row.attrName} 属性吗?`"
            @confirm="delAttr(row.id)"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" size="small"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts" setup>
import { watch, ref } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
import { getAttrListApi, delAttrApi } from "@/api/product/attr";
import type { AttrList, AttrItem } from "@/api/product/models/attrModels";
import { useAttrStore } from "@/stores/attr";

defineOptions({
  name: "AttrList",
});

// 4. 删除属性
const delAttr = async (id: number) => {
  await delAttrApi(id);
  ElMessage({
    type: "success",
    message: "删除属性成功",
  });
  getAttrList();
};

const attrStore = useAttrStore();

// 3. 显示修改属性
const showUpdateAttr = (row: AttrItem) => {
  emit("setIsShowAttrList", false);

  // 修改需要赋值
  attrStore.$patch({
    id: row.id,
    attrName: row.attrName,
    attrValueList: row.attrValueList,
  });
};

// 2. 显示添加属性
const emit = defineEmits(["setIsShowAttrList"]);

const showAddAttr = () => {
  emit("setIsShowAttrList", false);

  // 添加需要清空
  attrStore.$patch({
    id: undefined,
    attrName: "",
    attrValueList: [],
  });
};

// 1. 等三级分类都选中时，发送请求，获取属性列表展示（监视三级分类id）
const attrList = ref<AttrList>([]);
const categoryStore = useCategoryStore();

const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;

  if (!category3Id) {
    attrList.value = [];
    return;
  }

  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

watch(() => categoryStore.category3Id, getAttrList, {
  immediate: true,
});
</script>

<style scoped lang="scss">
.attr-list {
  margin-top: 20px;
}
</style>
